<?php


namespace app\index\controller;

use app\index\model\Vendor;
use think\Loader;
use think\Request;

class VendorController extends BaseController
{
    protected $request;
    protected $validate;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request=$request;
        $this->validate=Loader::validate('Vendor');
    }

    public function getVendorById(){
        $id=input('post.id');
        $vendor=Vendor::getVendorById($id);
        if(empty($vendor)) {
            return json(['code'=>1,'msg'=>'未找到信息']);
        }
        return json(['code' => 0, 'data' => $vendor]);
    }

    public function saveVendorInfo(){
        $data=input('post.');
        $validate_result=$this->validate->scene('save')->check($data);
        if(!$validate_result){
            return json(['code'=>1,'msg'=>$this->validate->getError()]);
        }
        $VendorClass=new Vendor();
        $result=$VendorClass->allowField(true)->save($data,['id'=>$data['id']]);
        if($result===false){
            return json(['code'=>1,'msg'=>'更新失败']);
        }
        return json(['code'=>0,'msg'=>'更新成功']);
    }
}